<template>
  <el-dialog :title="$i18n.t('repair')" :visible.sync="dialogVisible" append-to-body width="30%" @close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item :label="$i18n.t('defect.reviewed-by')" prop="receiveBy">
        <select-project-member ref="selectProjectMember" v-model="form.receiveBy" :project-id="projectId" :role-id="8" :is-head="false" :multiple="false" />
      </el-form-item>
      <el-form-item :label="$i18n.t('describe')">
        <el-input
          v-model="form.defectLogDescribe"
          type="textarea"
          rows="5"
          maxlength="255"
          :placeholder="$i18n.t('defect.enter-describe')"
          show-word-limit
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">{{ $i18n.t('submit') }}</el-button>
        <el-button @click="close">{{ $i18n.t('cancel') }}</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import SelectProjectMember from '@/components/Project/SelectProjectMember'
import { repair } from '@/api/system/defect'
export default {
  name: 'RepairDialog',
  components: { SelectProjectMember },
  props: {
    projectId: {
      type: Number,
      default: null
    },
    defectId: {
      type: Number,
      default: null
    },
    createById: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      dialogVisible: false,
      rules: {
        receiveBy: [
          { required: true, message: this.$t('defect.handle-by-cannot-empty'), trigger: 'change' }
        ]
      },
      form: {
        receiveBy: this.createById ? [this.createById] : [],
        remark: null
      }
    }
  },
  methods: {
    reset() {
      this.form = {
        receiveBy: [],
        remark: null
      }
      this.resetForm('form')
      this.$refs.selectProjectMember.clear()
    },
    open() {
      if (this.createById) {
        this.form.receiveBy = [this.createById]
      }
      this.dialogVisible = true
    },
    close() {
      this.dialogVisible = false
      this.reset()
    },
    onSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.defectId = this.defectId
          repair(this.defectId, this.form).then(res => {
            this.$modal.msgSuccess(this.$i18n.t('defect.repair-success'))
            this.close()
            this.$emit('log', res.data)
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
